<!DOCTYPE html>
<html>
    
    <head>
        <meta charset="utf-8">
        <title>
            后台管理系统
        </title>
        <meta name="renderer" content="webkit">
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
        <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="format-detection" content="telephone=no">
        <link rel="stylesheet" href="/static/css/x-admin.css" media="all">
    </head>
    
    <body>
        <div class="x-body">
            <form class="layui-form">
                <div class="layui-form-item">
                    <label for="username" class="layui-form-label">
                        <span class="x-red">*</span>图片
                    </label>

                    <div class="layui-input-inline">
                        <input type="file" name="photo" class="layui-upload-file">
                        <input type="hidden" name="img" id="img" value="">
                    </div>
                    <div class="layui-form-mid layui-word-aux">
                        <img src="" style="max-width: 100px" id="photos" alt="">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label for="link" class="layui-form-label">
                        <span class="x-red">*</span>地址
                    </label>
                    <div class="layui-input-block">
                        <input type="text" name="link" autocomplete="off" value="" placeholder="链接地址"
                               class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label for="link" class="layui-form-label">
                        <span class="x-red">*</span>位置
                    </label>
                    <div class="layui-input-block">
                        <select name="position" id="article_category_id">
                            <option value="">无</option>
                            <option value="1">首页标题</option>
                            <option value="2">banner轮播</option>
                            <option value="3">首页资讯</option>
                        </select>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label for="L_repass" class="layui-form-label">
                    </label>
                    <button  class="layui-btn" lay-filter="add" lay-submit="">
                        添加
                    </button>
                </div>
            </form>
        </div>
        <script src="/static/lib/layui/layui.js" charset="utf-8">
        </script>
        <script src="/static/js/x-layui.js" charset="utf-8">
        </script>
        <script>
            layui.use(['form','layer','upload'], function(){
                $ = layui.jquery;
              var form = layui.form()
              ,layer = layui.layer;
                layui.upload({
                    url: '{:url("upload")}'
                    ,success: function(res){
                        $('#img').val('/storage/'+res.imgpath);
                        $('#photos').attr('src','/storage/'+res.imgpath);//上传成功返回值，必须为json格式
                    }
                });
              //自定义验证规则
              form.verify({
                nikename: function(value){
                  if(value.length < 5){
                    return '昵称至少得5个字符啊';
                  }
                }
                ,pass: [/(.+){6,12}$/, '密码必须6到12位']
                ,repass: function(value){
                    if($('#L_pass').val()!=$('#L_repass').val()){
                        return '两次密码不一致';
                    }
                }
              });

              //监听提交
              form.on('submit(add)', function(data){
                  var ts ={
                      img:data.field.img,
                      link:data.field.link,
                      position:data.field.position
                  };
                  if(ts['img']=="")
                  {
                      layer.msg('上传图片不能为空',{icon:5,time:2000});return false;
                  }
                  if(ts['link']=="")
                  {
                      layer.msg('链接地址不能为空',{icon:5,time:2000});return false;
                  }
                  if(ts['position']=="")
                  {
                      layer.msg('位置不能为空',{icon:5,time:2000});return false;
                  }
                  $.ajax({
                      url:"{:url('bannerInserts')}",
                      data:ts,
                      type:'get',
                      success:function (res) {
                          if (res.code==100){
                              layer.msg(res.msg, {icon: 6,time:2000},function () {
                                  window.parent.location.reload();
                                  var index = parent.layer.getFrameIndex(window.name);
                                  parent.layer.close(index);
                              });
                              return false;
                          }else{
                              layer.msg(res.msg, {icon: 7,time:2000});
                              return false;
                          }
                      }
                  })
                return false;
              });
              
              
            });
        </script>
        
    </body>

</html>